package com.dragon.concurrent.demo.interrupt;

public class ThreadService {

    private Thread executeThread;

    private boolean flag;

    public void execute(Runnable runnable) {
        executeThread = new Thread(() -> {
            Thread daemonThread = new Thread(runnable);
            daemonThread.setDaemon(true);
            daemonThread.start();
            try {
                daemonThread.join();
            } catch (InterruptedException e) {
                System.out.println("");
            }
            flag = true;

        });
        executeThread.start();
    }

    public void shutdown(long mills) {
        long start = System.currentTimeMillis();
        while (!flag) {
            if (System.currentTimeMillis() - start > mills) {
                System.out.println("已经超时");
                executeThread.interrupt();
            }
        }
    }
}
